package test;

import org.example.util.DBUtil;
import org.junit.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

public class Jdbc02 {
    Connection conn=null;
    Statement statement=null;
    ResultSet resultSet=null;
    private static final String[] NAMES = {
            "Alice", "Bob", "Charlie", "David", "Eva", "Frank", "Grace", "Helen", "Ivy", "Jack"
    };
    public void init() throws ClassNotFoundException, SQLException {
        conn = DBUtil.getConnection();
        statement = conn.createStatement();
    }
    /**
     * 批量插入用户
     */
    @Test
    public void insertUserBatchTest() throws SQLException, ClassNotFoundException {
        init();
        StringBuilder sql = new StringBuilder("INSERT INTO user (name) VALUES ");
        int count = 100;
        Random random = new Random();

        for (int i = 0; i < count; i++) {
            String name = NAMES[random.nextInt(NAMES.length)];
            sql.append("('").append(name).append("')");

            if (i < count - 1) {
                sql.append(",");
            }
        }
        int result = statement.executeUpdate(sql.toString());
        System.out.println("插入了" + result + "条数据");
        DBUtil.close(statement, conn, resultSet);
    }
    /**
     * 批量删除
     */
    @Test
    public void deleteUserBatchTest() throws SQLException, ClassNotFoundException {
        init();
        StringBuilder sql = new StringBuilder("DELETE FROM user WHERE id IN (");
        int count = 200;
        for (int i = 101; i < count; i++) {
            sql.append(i);
            if (i < count - 1) {
                sql.append(",");
            }
        }
        sql.append(")");
        int result = statement.executeUpdate(sql.toString());
        System.out.println("删除了" + result + "条数据");
        DBUtil.close(statement, conn, resultSet);
    }
    /**
     * 全部查询
     */
    @Test
    public void selectAll() throws SQLException, ClassNotFoundException {
        init();
        String sql = "select id,name from user";
         resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println(id + " " + name);
        }
        DBUtil.close(statement, conn, resultSet);
    }
    /**
    *
     * 更新数据
     */
    @Test
    public void update() throws Exception {
        init();
        String sql = "update user set name = '张三' where id = 200";
        int count = statement.executeUpdate(sql);
        System.out.println(count);
        DBUtil.close(statement, conn, resultSet);
    }
   /**
    *
     * 批量更新数据
     */
    @Test
    public void updateBatch() throws Exception {
        init();

    }
    @Test
    public void login() throws SQLException, ClassNotFoundException {
        init();
        String username = "'张三'";
        String password = "'123456' or 1=1";
        String sql = "select * from user where name = "+username+" and password = "+ password;
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()){
            String name = resultSet.getString("name");
            String pwd = resultSet.getString("password");
            System.out.println(name+" "+pwd);
        }
        DBUtil.close(statement,conn,resultSet);


    }

}
